#include "grflib_stdafx.h"
#include "../include/VertexFormat.h"

namespace grflib
{
	namespace engine
	{
		namespace core
		{
			namespace vertex_format
			{
				bool Mapping(std::vector<D3D10_INPUT_ELEMENT_DESC> &elements,
							 std::size_t &vertexStride,
							 UINT nNormal,
							 UINT nTexCoord2D,
                             UINT nTexCoord3D)
				{
					const std::size_t FLOATSIZE = sizeof(float);
					vertexStride = FLOATSIZE * (3 /*pos*/ + 3 * nNormal + 2 * nTexCoord2D + 3 * nTexCoord3D);

					elements.clear();

					D3D10_INPUT_ELEMENT_DESC desc[] = 
					{{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
					 {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
					 {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
					 {"TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}};

					elements.push_back(desc[0]); //POSITION IS A MUST!
					UINT offset = FLOATSIZE * 3;

					for (UINT i = 0; i < nNormal; i++) //N normals
					{
						desc[1].SemanticIndex = i;
						desc[1].AlignedByteOffset = offset;
						elements.push_back(desc[1]);
						offset += FLOATSIZE * 3;
					}

					for (UINT i = 0; i < nTexCoord2D; i++)
					{
						desc[2].SemanticIndex = i;
						desc[2].AlignedByteOffset = offset;
						elements.push_back(desc[2]);
						offset += FLOATSIZE * 2;
					}

					for (UINT i = 0; i < nTexCoord3D; i++)
					{
						desc[3].SemanticIndex = i + nTexCoord2D;
						desc[3].AlignedByteOffset = offset;
						elements.push_back(desc[3]);
						offset += FLOATSIZE * 3;
					}

                    return true;
				}

			} //namespace vertex_format
		} //namespace core
	} //namespace engine
} //namespace grflib